package com.admin.web.controller;

import com.admin.web.model.AjaxJson;
import com.admin.web.uresponse.OBGUserResponse;
import com.admin.web.verify.ParamVerify;
import com.bayss.core.constant.ResultMsg;
import com.bayss.core.entity.homepage.ActivityApplyInfo;
import com.bayss.core.entity.homepage.ActivityInfo;
import com.bayss.core.entity.homepage.RecommendInfo;
import com.bayss.core.entity.homepage.RotationChart;
import com.bayss.core.entity.rbac.OBGUserInfo;
import com.bayss.core.service.homepage.ActivityService;
import com.bayss.core.service.homepage.RecommendService;
import com.bayss.core.service.homepage.RotationChartService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * program: doko
 * author: bsworld.xie
 * create: 2019-11-10 16:26
 * description:
 */
@RestController
public class HomepageController  extends ApiBaseController{
    @Autowired
    ActivityService activityService;
    @Autowired
    RotationChartService rotationChartService;
    @Autowired
    RecommendService recommendService;

    /**
     * 添加活动
     */
    @PostMapping(value = "/homepage/activity/add.json")
    public ResultMsg activityAdd(OBGUserResponse obgUserResponse, @RequestBody ActivityInfo activityInfo) {
        activityInfo.setOperatorId(obgUserResponse.getUid());
        return activityService.addActivity(activityInfo);
    }

    /**
     * 活动更新
     */
    @RequestMapping(value = "/homepage/activity/update.json", method = RequestMethod.POST)
    public ResultMsg activityUpdate(@RequestBody ActivityInfo activityInfo) {
        activityService.updateActivity(activityInfo);
        return ResultMsg.success();
    }

    /**
     * 根据id查询活动
     */
    @RequestMapping(value = "/homepage/activity/getById.json", method = RequestMethod.GET)
    public ResultMsg activityGetById(Integer id) {
        if (id == null) {
            return ResultMsg.fail(-1,"id为空");
        }
        ActivityInfo activityInfo = activityService.getActivityById(null,id);
        return ResultMsg.success(activityInfo);
    }

    /**
     * 根据id查询活动
     */
    @RequestMapping(value = "/homepage/activity/deleteById.json", method = RequestMethod.GET)
    public ResultMsg activityDeleteById(Integer id) {
        if (id == null) {
            return ResultMsg.fail(-1, "id为空");
        }
        activityService.deleteByPrimaryKey(id);
        return ResultMsg.success();
    }

    /**
     * 分页查询活动
     */
    @RequestMapping(value = "/homepage/activity/getByPage.json", method = RequestMethod.GET)
    public ResultMsg activityGetByPage(Integer page, Integer pageSize) {
        try {
            PageInfo<ActivityInfo> pageInfo = activityService.getActivityByPage(page, pageSize, null, false);
            return ResultMsg.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(-1, "失败");
        }
    }

    /**
     * 分页查询活动报名信息
     */
    @RequestMapping(value = "/homepage/activity/applyGetByPage.json", method = RequestMethod.GET)
    public ResultMsg activityApplyGetByPage(Integer activityId,Integer page, Integer pageSize) {
        try {
            PageInfo<ActivityApplyInfo> pageInfo = activityService.findPages(activityId, page, pageSize);
            return ResultMsg.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(-1, "失败");
        }
    }

    /**
     * 已参加活动统计
     * @param activityId
     * @return
     */
    @RequestMapping(value = "/homepage/activity/applyCount.json", method = RequestMethod.GET)
    public ResultMsg applyCount(Integer activityId) {
        try {
            return ResultMsg.success(activityService.countApplyAmount(activityId));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(-1, "失败");
        }
    }
    /**
     * 轮播图添加
     */
    @RequestMapping(value = "/homepage/rotationChart/add.json", method = RequestMethod.POST)
    public ResultMsg rotationChartAdd(OBGUserResponse obgUserResponse,@RequestBody RotationChart activityInfo) {
        activityInfo.setCreator(obgUserResponse.getObgUserInfo().getNickname());
        activityInfo.setOperatorId(obgUserResponse.getUid());
        rotationChartService.addRotationChart(activityInfo);
        return ResultMsg.success();
    }

    /**
     * 轮播图更新
     */
    @RequestMapping(value = "/homepage/rotationChart/update.json", method = RequestMethod.POST)
    public ResultMsg rotationChartUpdate(OBGUserResponse obgUserResponse,@RequestBody RotationChart activityInfo) {
        activityInfo.setOperatorId(obgUserResponse.getUid());
        activityInfo.setOperatorName(obgUserResponse.getObgUserInfo().getNickname());
        rotationChartService.updateRotationChart(activityInfo);
        return ResultMsg.success();
    }

    /**
     * 轮播图删除
     */
    @RequestMapping(value = "/homepage/rotationChart/deleteById.json", method = RequestMethod.GET)
    public ResultMsg rotationChartDeleteById(Integer id) {
        if (id == null) {
            return ResultMsg.fail(-1,"id为空");
        }
        rotationChartService.deleteById(id);
        return ResultMsg.success();
    }


    /**
     * 根据id获取轮播图信息
     */
    @RequestMapping(value = "/homepage/rotationChart/getById.json", method = RequestMethod.GET)
    public ResultMsg rotationChartGetById(Integer id) {
        RotationChart rotationChart = rotationChartService.getRotationChartById(id);
        return ResultMsg.success( rotationChart);
    }

    @RequestMapping(value = "/homepage/rotationChart/getByPage.json", method = RequestMethod.GET)
    public ResultMsg rotationChartGetByPage(Integer page, Integer pageSize) {
        PageInfo<RotationChart> rotationChartByPage = rotationChartService.getRotationChartByPage(null,page, pageSize);
        return ResultMsg.success( rotationChartByPage);
    }

    /**
     * 推荐相关信息添加
     */
    @RequestMapping(value = "/homepage/recommendInfo/add.json", method = RequestMethod.POST)
    public ResultMsg recommendInfoAdd(OBGUserResponse obgUserResponse,
            @ParamVerify(isNumber = true, isNotNull = true) Integer skuId,
            @ParamVerify(isNumber = true, isNotNull = true) Integer weight,
            @ParamVerify(isNotNull = true) String bigPicCn,
            @ParamVerify(isNotNull = true) String bigPicEn,
            @ParamVerify(isNotNull = true) String bigPicKo,
            @ParamVerify(isNotNull = true) String littlePicCn,
            @ParamVerify(isNotNull = true) String littlePicEn,
            @ParamVerify(isNotNull = true) String littlePicKo
    ) {
        RecommendInfo build = RecommendInfo.builder().bigPicCn(bigPicCn)
                .bigPicEn(bigPicEn)
                .bigPicKo(bigPicKo)
                .littlePicCn(littlePicCn)
                .littlePicEn(littlePicEn)
                .littlePicKo(littlePicKo)
                .skuId(skuId)
                .weight(weight)
                .creator(obgUserResponse.getObgUserInfo().getNickname())
                .operatorId(obgUserResponse.getUid())
                .build();

        recommendService.addRecommendInfo(build);
        return ResultMsg.success();
    }

    /**
     * 推荐相关信息更新
     */
    @RequestMapping(value = "/homepage/recommendInfo/update.json", method = RequestMethod.POST)
    public ResultMsg recommendInfoUpdate(OBGUserResponse obgUserResponse,RecommendInfo recommendInfo) {
        recommendInfo.setOperatorId(obgUserResponse.getUid());
        recommendInfo.setOperatorName(obgUserResponse.getObgUserInfo().getNickname());
        recommendService.updateRecommendInfo(recommendInfo);
        return ResultMsg.success();
    }

    /**
     * 推荐相关删除
     */
    @RequestMapping(value = "/homepage/recommendInfo/delete.json", method = RequestMethod.GET)
    public ResultMsg recommendInfoDelete(Integer id) {
        recommendService.deleteRecommendInfo(id);
        return ResultMsg.success();
    }

    /**
     * 餐品推荐 禁用、启用
     *
     * @param id
     */
    @RequestMapping(value = "/homepage/recommendInfo/updateStatus.json", method = RequestMethod.POST)
    public ResultMsg recommendInfoDelete(Integer id, Integer status) {
        recommendService.updateStatus(id, status);
        return ResultMsg.success();
    }

    /**
     * 根据id 获取推荐相关信息
     */
    @RequestMapping(value = "/homepage/recommendInfo/getById.json", method = RequestMethod.GET)
    public ResultMsg recommendInfoGetById(Integer id) {
        RecommendInfo recommendInfo = recommendService.getRecommendInfoById(id);
        return ResultMsg.success(recommendInfo);
    }

    /**
     * 分页获取推荐相关信息
     */
    @RequestMapping(value = "/homepage/recommendInfo/getByPage.json", method = RequestMethod.GET)
    public ResultMsg recommendGetByPage(Integer page, Integer pageSize) {
        List<RecommendInfo> recommendInfoByPage = recommendService.getRecommendInfoByPage(null,page, pageSize);
        return ResultMsg.success(recommendInfoByPage);
    }

}